<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
    <title>Agent新增</title>
</head>
<link rel="stylesheet" type="text/css" href="css/ext-all.css"/>
<link rel="stylesheet" type="text/css" href="css/ext-zh_CN.css"/>
<script type="text/javascript" src="js/ext-all.js"></script>
<script type="text/javascript" src="js/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/base.js"></script>
<body>
<script type="text/javascript">
    Ext.onReady(function () {
        Ext.getBody().mask('加载中...');

        var buttonPanel = Ext.create('Ext.Panel', {
            id: 'buttonPanel',
            defaults: {
                style: 'margin: 2px;'
            },
            items: [{
                id: 'save',
                xtype: 'button',
                text: '保存',
                handler: _insertAgent
            }, {
                id: 'close',
                xtype: 'button',
                text: '关闭',
                handler: _close
            }]
        });

        var formPanel = Ext.create('Ext.form.Panel', {
            id: 'formPanel',
            layout: 'column',
            frame: true,
            defaults: {
                labelAlign: 'right',
                labelWidth: 140,
                inputWidth: 240,
                margin: '4,0,0,0'
            },
            items: [{
                xtype: 'textfield',
                name: 'AGENT_CODE_',
                id:'AGENT_CODE_',
                fieldLabel: '智能体编码',
                maxLength: 40,
                allowBlank: false
            }, {
                xtype: 'textfield',
                id:'AGENT_NAME_',
                name: 'AGENT_NAME_',
                fieldLabel: '智能体名称',
                maxLength: 40,
                allowBlank: false
            }, {
                xtype: 'textfield',
                id:'AGENT_URL_',
                name: 'AGENT_URL_',
                fieldLabel: '智能体路径',
                maxLength: 100,
                allowBlank: false
            }, {
                xtype: 'textfield',
                id:'AGENT_ARGUMENTS_',
                name: 'AGENT_ARGUMENTS_',
                fieldLabel: '智能体启动参数',
                maxLength: 40
            }, {
                xtype: 'numberfield',
                id:'AGENT_COST_',
                name: 'AGENT_COST_',
                fieldLabel: '设备智能体价值(元)',
                minValue: 0,
                hideTrigger: true,
                allowDecimals: false,
                maxLength: 40
            }, {
                xtype: 'textfield',
                id:'AGENT_SOURCE_',
                name: 'AGENT_SOURCE_',
                fieldLabel: '设备智能体所属',
                maxLength: 40
            }]
        });

        Ext.create('Ext.container.Viewport', {
            layout: {
                type: 'border',
                regionWeights: {
                    west: -1,
                    north: 1,
                    south: 1,
                    east: -1
                }
            },
            defaults: {
                border: false
            },
            items: [{
                region: 'north',
                items: [buttonPanel]
            }, {
                region: 'center',
                layout: 'fit',
                items: [formPanel]
            }]
        });

        _init();
    });

    function _init() {
        Ext.getBody().unmask();
    }

    function _insertAgent() {
        var formPanel = Ext.getCmp('formPanel').getForm();
        if (formPanel.isValid() == false) {
            Ext.MessageBox.alert('警告', '数据不符合规范', Ext.MessageBox.WARNING);
            return;
        }
        //校验路径是否符合规范
        var url = Ext.getCmp('AGENT_URL_').getValue();
        if (url.indexOf('/') != -1 || url.indexOf('\\') != -1) {
            Ext.MessageBox.alert('警告', '路径不符合规范,请用"."分隔', Ext.MessageBox.WARNING);
            return;
        }
        Ext.getCmp('formPanel').getForm().submit({
            url: 'insertAgent',
            submitEmptyText: false,
            waitMsg: '处理中...',
            success: function (form, action) {
                var data = action.result;
                parent.returnValue = data.result;
                _close();
            },
            failure: function (form, action) {
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.MessageBox.alert('错误', '请求失败', Ext.MessageBox.ERROR);
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.MessageBox.alert('错误', action.result.message, Ext.MessageBox.ERROR);
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.MessageBox.alert('错误', '服务错误', Ext.MessageBox.ERROR);
                }
            }
        });
    }

    function _close() {
        parent.win.close();
    }
</script>
</body>
</html>
